import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;


public class ClientThread extends Thread{

	private Socket socket = null;
	
	private Simulator sim;
	
	private int id;

    public ClientThread(Socket socket, Simulator current) {
    	super("ClientThread");
    	this.socket = socket;
    	this.sim = current;
    }

    public void run() {

    	try {
    		
    		BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    		PrintWriter out = new PrintWriter(this.socket.getOutputStream(), true);
    		String inline;
    		
    		//System.out.println(inline); // Teste
    		
    		while ((inline = in.readLine()) != null) {
    			
    			System.out.println(inline);
    			
    			if(inline.contains("RegisterVehicle")){
        			
    				/** RegisterVehicle <id> <netport> <emulport> <battery> <StationId> **/
    				
    				String[] split = inline.split(" ");
    				
        			int ide = Integer.parseInt(split[1]);
        			int netport = Integer.parseInt(split[2]);
        			int emulport = Integer.parseInt(split[3]);
        			int batt = Integer.parseInt(split[4]);
        			int station_id = Integer.parseInt(split[5]);
        			
        			split = this.socket.getRemoteSocketAddress().toString().split(":");
        			
        			String address = split[0].substring(1);
        			
        			Machine a = null;
        			Location initial =  null;
        			
        			Station sta = sim.findStation(station_id);
        			
        			if(sta != null){
        				
        				System.out.println("Vehicle "+ide+" on Station "+station_id);
        				
        				initial = new Location(sta.getLatitude(), sta.getLongitude(), 0);
	        			
        				a = new Machine(ide,initial, emulport, out, address, netport, batt, 0);
        			}
        			else{

	        				initial = new Location(38.7370800, -9.1334152, 0);
	        			
	        				a = new Machine(ide,initial, emulport, out, address, netport, batt, 0);
        			}
        			this.id = ide;
    				a.getSimOut().println("Address "+address);
    				a.getEmulOut().println("redir add tcp:"+netport+":10000");
    				a.getEmulOut().println("geo fix "+ initial.getLongitude()+" "+initial.getLatitude()+" "+initial.getAltitude());
    				this.sim.addMachine(a);
        		}else if(inline.contains("Move")){
        			
        			String[] split = inline.split(" ");
        			
        
        			
        			double lat = Double.parseDouble(split[2]);
        			double lon = Double.parseDouble(split[1]);
        			
        			Location destiny = new Location(lat, lon, 0);
        			
        			this.sim.findMachine(this.id).normalize();
        			this.sim.findMachine(this.id).Start(destiny);
        			
        			
        		}else if(inline.contains("UnregisterVehicle")){
        			
        			String[] split = inline.split(" ");
        			
        			//String Sid = inline.substring(11);
        			int ide = Integer.parseInt(split[1]);
        			Machine mach = this.sim.findMachine(ide);
        			mach.getEmulOut().println("redir del tcp:"+mach.getPort());
        			sim.removeMachine(ide);
        			
        		}else if(inline.contains("RegisterStation")){
                    
                    /* Register <id> <netport> <emulport> <lon> <lat>*/
                    
                    String[] split = inline.split(" ");
                    
                    int idS = Integer.parseInt(split[1]);
                    int netport = Integer.parseInt(split[2]);
                    int emulport = Integer.parseInt(split[3]);
                    double lon = Double.parseDouble(split[4]);
                    double lat = Double.parseDouble(split[5]);
                    
                    split = this.socket.getRemoteSocketAddress().toString().split(":");
                    
                    String address = split[0].substring(1);
                    
                    Station b = null;
                    //Location initial =  new Location(lat, lon, 0);
                    b = new Station(idS, lat, lon, emulport, out, address, netport);
                    this.sim.addStation(b);
                    b.getSimOut().println("Address "+address);
    				b.getEmulOut().println("redir add tcp:"+netport+":10000");
    				
                } else if(inline.contains("UnregisterSation")){
        			
        			String[] split = inline.split(" ");
        			
        			int ide = Integer.parseInt(split[1]);
        			System.out.println("estou aqui!: " + ide);
        			
        			Station sta = this.sim.findStation(ide);
        			sta.getEmulOut().println("redir del tcp:"+sta.getPort());
        			sim.removeStation(ide);
        			
        		}else if(inline.contains("Rise_Altitude")){
        			
        			this.sim.findMachine(this.id).riseAltitude();
    		
        		}else if(inline.contains("Normalize")){
        			
        			this.sim.findMachine(this.id).normalize();
        		}else if(inline.contains("Set_Battery")){
        			
        			String[] split = inline.split(" ");
        			this.sim.findMachine(id).setBattery(Integer.parseInt(split[1]));
        		}else if(inline.contains("Reset_Battery")){
        			
        			this.sim.findMachine(this.id).setBattery(0);
        		}
    		}
    		
    		socket.close();
    		
    		
    		

    	} catch (IOException e) {
    		System.out.println("Algum erro");
    		e.printStackTrace();
    	}
    	
    }
	
	
}
